from sqlalchemy import select

from src.application.server.dto import ServerDTO
from src.application.server.exceptions import ServerIdNotExist
from src.application.server.interfaces import ServerReader
from src.infrastructure.db.converters.server import convert_db_model_to_server_dto
from src.infrastructure.db.models import Server
from src.infrastructure.db.repositories.base import SQLAlchemyRepo


class ServerReaderImpl(SQLAlchemyRepo, ServerReader):
    async def get_server_by_id(self, server_id: int) -> ServerDTO:
        query = select(Server).where(Server.id == server_id)

        server: Server = await self._session.scalar(query)

        if server is None:
            raise ServerIdNotExist(server_id)

        return convert_db_model_to_server_dto(server)
